package com.xiaolong.nacos.config;



import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.NacosServiceManager;
import com.alibaba.cloud.nacos.discovery.configclient.NacosDiscoveryClientConfigServiceBootstrapConfiguration;
import com.alibaba.cloud.nacos.registry.NacosRegistration;
import com.alibaba.cloud.nacos.registry.NacosServiceRegistry;
import com.xiaolong.cloud.feign.config.FeignClientAutoConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;


@Configuration(
        proxyBeanMethods = false
)
@AutoConfiguration
@AutoConfigureAfter(value = {NacosLoadBalancerConfig.class, FeignClientAutoConfig.class, NacosDiscoveryClientConfigServiceBootstrapConfiguration.class})
@LoadBalancerClients(defaultConfiguration = NacosLoadBalancerConfig.class)
@ImportAutoConfiguration(NacosLoadBalancerConfig.class)
public class NacosApplicationConfig {

    @Value("${spring.cloud.loadBalancer.weight:10}")
    private String weight;

    @Value("${spring.cloud.loadBalancer.region:CN}")
    private String region;


    @Primary
    @Bean("NacosServiceRegistry")
    public NacosServiceRegistry nacosServiceRegistry(NacosServiceManager nacosServiceManager, NacosDiscoveryProperties nacosDiscoveryProperties) {
        return new NacosServiceRegistry(nacosServiceManager, nacosDiscoveryProperties) {
            @Override
            public void register(Registration registration) {
                if (registration instanceof NacosRegistration) {
                    NacosRegistration nacosRegistration = (NacosRegistration) registration;
                    // 设置权重
                    nacosRegistration.getMetadata().put("weight", weight);
                    // 设置区域
                    nacosRegistration.getMetadata().put("region", region);
                }
                super.register(registration);
            }
        };
    }

//    @Bean
//    public NacosServiceRegistryWrapper nacosServiceRegistryWrapper(NacosServiceRegistry originalRegistry) {
//        return new NacosServiceRegistryWrapper(originalRegistry);
//    }
//
//    class NacosServiceRegistryWrapper implements ServiceRegistry {
//        private final NacosServiceRegistry delegate;
//
//        NacosServiceRegistryWrapper(NacosServiceRegistry delegate) {
//            this.delegate = delegate;
//        }
//
//        @Override
//        public void register(Registration registration) {
//            if (registration instanceof NacosRegistration) {
//                NacosRegistration nacosRegistration = (NacosRegistration) registration;
//                nacosRegistration.getMetadata().put("weight", "10");
//                nacosRegistration.getMetadata().put("region", "us-west");
//            }
//            delegate.register(registration);
//        }
//
//        @Override
//        public void deregister(Registration registration) {
//
//        }
//
//        @Override
//        public void close() {
//
//        }
//
//        @Override
//        public void setStatus(Registration registration, String status) {
//
//        }
//
//        @Override
//        public Object getStatus(Registration registration) {
//            return null;
//        }
//
//        // Delegate other methods to the original NacosServiceRegistry
//        // ...
//    }
}
